<template>
  <div class="TopData">
    <div>
      <HmButton>
      </HmButton>
    </div>
    <!-- 标题 -->
    <!-- <div class="title">中国各省份人口流向统计系统</div> -->

    <div class="escb">
      <el-button class="esc-button" @click="esc">返回</el-button>
    </div>

    <!-- 城市卡片列表 -->
    <div class="cards">
      <div v-for="city in cities" :key="city.name" class="card">
        <div class="statistic-card">
          <h3>{{ city.name }}</h3>
          <div class="statistic-item">
            <span>人口涌入:</span>
            <span :id="city.name + '-inflow'" class="number"></span>
          </div>
          <div class="statistic-item">
            <span>人口流失:</span>
            <span :id="city.name + '-outflow'" class="number"></span>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import { CountUp } from 'countup.js';

export default {
  data() {
    return {
      cities: [
        { name: '北京', inflow: 5000, outflow: 3000 },
        { name: '上海', inflow: 4500, outflow: 2800 },
        { name: '广州', inflow: 3800, outflow: 2500 },
        { name: '深圳', inflow: 4200, outflow: 2700 }
      ],
      countUpInstances: {}
    };
  },
  mounted() {
    this.cities.forEach(city => {
      const inflowId = `${city.name}-inflow`;
      const outflowId = `${city.name}-outflow`;

      // 初始化涌入动画
      const inflowCountUp = new CountUp(inflowId, 0, city.inflow, { duration: 2 });
      inflowCountUp.start();
      this.countUpInstances[inflowId] = inflowCountUp;

      // 初始化流失动画
      const outflowCountUp = new CountUp(outflowId, 0, city.outflow, { duration: 2 });
      outflowCountUp.start();
      this.countUpInstances[outflowId] = outflowCountUp;
    });

    setInterval(this.updateStatistics, 3000);
  },
  methods: {
    updateStatistics() {
      this.cities.forEach(city => {
        const inflow = Math.floor(Math.random() * 10000);
        const outflow = Math.floor(Math.random() * 10000);

        const inflowId = `${city.name}-inflow`;
        const outflowId = `${city.name}-outflow`;

        // 更新动画
        this.countUpInstances[inflowId].update(inflow);
        this.countUpInstances[outflowId].update(outflow);

        // 更新数据
        city.inflow = inflow;
        city.outflow = outflow;
      });
    },
    esc() {
      this.$router.push('/');
    },

  }
};
</script>

<style scoped>
.escb {
  position: absolute;
  left: 40px;

}

.TopData {
  max-width: 1000px;
  margin: 0 auto;
}

.title {
  font-size: 26px;
  color: white;
  font-weight: bold;
  padding: 20px;
  text-align: center;
  margin-bottom: 10px;
}

.cards {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.card {
  flex: 1 1 22%;
  min-width: 200px;
  margin: 10px;
}

.statistic-card {
  background: linear-gradient(135deg, #003287, #001F3F);
  opacity: 0.8;
  padding: 20px;
  border-radius: 8px;
  color: #fff;
  text-align: center;
}

.statistic-item {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.number {
  font-size: 24px;
  font-weight: bold;
  color: #1abc9c;
}
</style>